<?php


namespace app\api\controller;
use app\api\model\Base as bs;
use think\Db;
use think\Request;
class Course extends Base
{
    public function course(){
//        if($this->request->method() == 'GET'){
        $head = $this->request = Request::instance()->header();
        if(substr($head['token'], -1)==4){
            $this->apiReturn(101,'敢越过权限，你胆子蛮大嘛');
        }
        if(Request::instance()->method()=="GET"){
            $page = input('current','1','intval');
            $page_size = input('pageSize',10,'intval');
            $like = I("course_name|department_name|people_num|time|classroom_name|teacher_name");
            $data = Db::name('course')
                ->alias('a')
                ->join('course_department b','a.id = b.course_id')
                ->join('department c','b.department_id = c.id')
                ->join('course_classroom d','a.id = d.course_id')
                ->join('classroom e','d.classroom_id = e.id')
                ->join('user f','a.teacher_id = f.id','INNER')
                ->field(['e.id'=>'classroom_id','e.name'=>'classroom_name'])
                //->field('a.course_name,a.people_num,a.time,c.id,c.department_name,e.id,e.name')->field(['a.id'=>'key'])
                ->field('a.course_name,a.people_num,a.time,c.department_name')
                ->field(['a.id'=>'key','c.id'=>'department_id'])
                ->field(['f.username'=>'teacher_name','f.id'=>'teacher_id'])
                ->where('a.course_name', 'like', '%'.$like['course_name'].'%')
                ->where('c.department_name', 'like', '%'.$like['department_name'].'%')
                ->where('a.people_num', 'like', '%'.$like['people_num'].'%')
                ->where('a.time', 'like', '%'.$like['time'].'%')
                ->where('e.name', 'like', '%'.$like['classroom_name'].'%')
                ->where('f.username', 'like', '%'.$like['teacher_name'].'%')
                ->page($page,$page_size)
                ->where('a.status',1)
                 ->select();
            $total = bs::total('course');
            $this->apiReturn(200,'',$data,$total);
        }


        if(Request::instance()->method()== 'POST'){
//            {
//                "classroom_id": 2,
//                "course_name": "软件技术",
//                "people_num": 40,
//                "time": "14.00-15.30",
//                "department_name": "会计系",
//                "department_id": 2
//            }
            $value = I('course_name|department_id|classroom_id|people_num|time|teacher_id');
            foreach ($value as $k=>$v){
                if(!$v){
                    $this->apiReturn(303,'lack '.$k);
                }
            }
            Db::startTrans();
            try{
                $course = Db::name('course')->insertGetId([
                    'course_name'=>$value['course_name'],
                    'people_num'=>$value['people_num'],
                    'time'=>$value['time'],
                    'create_time' => time(),
                    'update_time' => time(),
                    'teacher_id'=>$value['teacher_id']
                ]);
                $course_classroom = Db::name('course_classroom')->insertGetId([
                    'classroom_id'=>$value['classroom_id'],
                    'course_id'=>$course,
                ]);
                $course_department = Db::name('course_department')->insertGetId([
                    'department_id'=>$value['department_id'],
                    'course_id'=>$course,
                ]);

                if($course && $course_classroom && $course_department){
                    Db::commit();
                    $this->apiReturn(200,'',['id'=>$course]);
                    return true;
                }
            }catch (Exception $e){
                Db::rollback();
                return $e->getMessage();
            }

        }
        if(Request::instance()->method()== 'PUT'){
            $value = I('id|course_name|department_id|classroom_id|people_num|time');
            foreach ($value as $k=>$v){
                if(!$v){
                    $this->apiReturn(303,'lack '.$k);
                }
            }
            $value['update_time'] = time();


            Db::startTrans();
            try{
                $course = bs::edit('course',$value['id'],[
                    'course_name'=>$value['course_name'],
                    'people_num'=>$value['people_num'],
                    'time'=>$value['time'],
                    'update_time' => time()
                ]);

                $course_classroom = Db::name('course_classroom')->where(['course_id'=>$value['id']])->update([
                    'classroom_id'=>$value['classroom_id'],
                ]);
                $course_department = Db::name('course_department')->where(['course_id'=>$value['id']])->update([
                    'department_id'=>$value['department_id'],
                ]);
               //因为未修改数据，返回0，我这三个表，所以我没用if
//                if($course && $course_classroom && $course_department){
//                    Db::commit();
//                    $this->apiReturn(200,'',['id'=>$value['id']]);
//                    return true;
//                }
                Db::commit();
                $this->apiReturn(200,'',['id'=>$value['id']]);
                return true;
            }catch (Exception $e){
                Db::rollback();
                $this->apiReturn(303,'',['id'=>$value['id']]);
                //return $e->getMessage();
            }
        }
        if(Request::instance()->method()== 'DELETE'){
            $id = input('id');
            if(!$id){
                $this->apiReturn(303,'lack id');
            }
            $course = bs::del('course',$id);
            $course_classroom = bs::del('course_classroom',$course,'course_id');
            $course_department = bs::del('course_department',$course,'course_id');
            if($course && $course_classroom && $course_department){
                $this->apiReturn(200,'',$course);
            }
        }
    }

}